﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace y12fjydbs_1
{
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public partial class FrmYDL : Form
    {
        #region 变量定义

        /// <summary>
        /// 烟弹数量
        /// </summary>
        int cYDNum = 208; //烟弹数量

        /// <summary>
        /// 命令发送次数
        /// </summary>
        int cCmdTimes = 0;

        private string NowTimeStr;


        CmdType cCmdType = CmdType.None;


        List<CheckBox> clistCheckBox = new List<CheckBox>();
        List<YDStatus> clistYDStatus = new List<YDStatus>();

        List<CheckBox> cListRowSelect = new List<CheckBox>();

        bool cCheckKZQStatus = false; //查询控制器状态 

        bool cIsFristRun = true;

        delegate void SetTextCallback(string text, Color TextColor);

        private System.Timers.Timer CmdTimer = new System.Timers.Timer();
        private System.Timers.Timer CmdTimerCX = new System.Timers.Timer();

        MySerialData cSerialData;

        #endregion

        #region 焰弹 --- 焰条 切换按钮
        private void BtnYT_Click(object sender, EventArgs e)
        {
            if (MyGlobal.frmYT == null)
            {
                MyGlobal.frmYT = new FrmYT();
                MyGlobal.frmYT.Show();
            }
            else
            {
                MyGlobal.frmYT.Show();
            }
        }
        #endregion

        #region FrmYDL 构造函数
        public FrmYDL()
        {
            InitializeComponent();
            MyGlobal.frmYDL = this;
            cSerialData = MyGlobal.GetSerialData(); 
             
        }
        #endregion
         
        #region FrmYDL_Load
        private void FrmYDL_Load(object sender, EventArgs e)
        {

            button1.Text = "启动";
            timer1.Enabled = false;
        }

        #endregion

        #region FrmYDL_FormClosing
        private void FrmYDL_FormClosing(object sender, FormClosingEventArgs e)
        {
            button1.Text = "停止";
            cSerialData.cStop = true;
            Application.Exit();
        }
        #endregion

        int cSendCount = 1;

        #region 界面时间刷新定时器

        private void timer1_Tick(object sender, EventArgs e)
        {
            DateTime myDateTime = DateTime.Now;
 
            NowTimeStr = myDateTime.ToString("    [HH:mm:ss]   ");
            label3.Text = myDateTime.ToString("yyyy年MM月dd日 HH时mm分ss秒");

            RecviveMsg($">> {cSendCount++}", Color.LightSalmon);
            cSerialData.SendCmd_CX(CmdByte.YD, 0x00, 0x00);
        }
        #endregion
         


        #region RecviveMsg
        public void RecviveMsg(string text, Color TextColor)
        {
            if (InfoDisp.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(RecviveMsg);
                InfoDisp.Invoke(d, new object[] { text, TextColor });
            }
            else
            {
                text = NowTimeStr + text + " \n";
                InfoDisp.Focus();
                //设置光标的位置到文本尾    
                InfoDisp.Select(InfoDisp.TextLength, 0);
                //滚动到控件光标处    
                InfoDisp.SelectionFont = new Font("微软雅黑", 10.5F, FontStyle.Regular);
                InfoDisp.SelectionColor = TextColor;
                InfoDisp.AppendText(text);
                InfoDisp.ScrollToCaret();
                InfoDisp.Update();
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Interval = int.Parse(textBox1.Text);
            timer1.Enabled = true;
             
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;

            Debug.WriteLine(DateTime.UtcNow);
        }
    }
}
